package com.cn.yds.bdy.dao;

import java.util.List;

import com.cn.yds.bdy.dao.model.OPtModel;
import com.cn.yds.bdy.dao.model.OptPo;
import com.cn.yds.bdy.dao.model.Topic;
import com.cn.yds.bdy.dao.model.TopicModel;
import com.cn.yds.bdy.dao.model.UserModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface OptRepository extends JpaRepository<OPtModel, Integer> {

    List<OPtModel> findByTopicId(Integer topicId);

    List<OPtModel> findByStudentIdAndStatus(Integer studentId, Integer status);

    List<OPtModel> findByIdInAndStatus(List<Integer> optIds, Integer status);

    List<OPtModel> findByTopicIdInAndStatus(List<Integer> topicIds, Integer status);

    @Query(value = "SELECT NEW com.cn.yds.bdy.dao.model.Topic(a.id,b.teacherId,b.topicName,b.description,c.name,"
            + "d.value, e.value,c.tel) FROM OPtModel a "
            + "JOIN TopicModel b ON a.topicId= b.id "
            + "JOIN UserModel c on b.teacherId=c.id AND c.role=0 "
            + "JOIN DictionaryModel d ON d.id = b.status AND d.type =2 "
            + "JOIN DictionaryModel e ON e.code = a.teacherOpinion AND e.type =3 "
            + "WHERE a.studentId = ?1 and a.status =1  AND (b.topicName like %?2% OR b.description like %?2% OR c.name like %?2%)")
    Page<Topic> findStudentSelectedTopic(Integer studentId, String searchStr, Pageable pageable);

    @Query(value = "SELECT b FROM OPtModel a JOIN TopicModel b on a.topicId= b.id and b.teacherId =:teacherId "
            + "WHERE a.teacherOpinion =:opinion")
    List<TopicModel> findByTeacherIdAndTeacherOpinion(@Param("teacherId") Integer teacherId, @Param("opinion") Integer opinion);


    @Query(value = "SELECT b FROM OPtModel a JOIN UserModel b ON a.studentId = b.id AND b.role = 1 "
            + "WHERE a.teacherOpinion=:opinion AND a.topicId =:topicId AND b.chooseStatus =:chooseStatus")
    List<UserModel> getStudentsOfTopic(@Param("topicId") Integer topicId, @Param("opinion") Integer opinion,
                                       @Param("chooseStatus") Integer chooseStatus);


    @Modifying
    @Query(value = "update OPtModel set teacherOpinion =1  " +
            "where studentId =:studentId and topicId =:topicId " +
            "and not exists (select 1 from UserModel where id =:studentId and  chooseStatus =1)")
    int teacherAccepteStudent(@Param("studentId") Integer studentId, @Param("topicId") Integer topicId);


    @Modifying
    @Query(value = "update OPtModel set teacherOpinion = -1 where topicId =:topicId and studentId <>:studentId")
    int rejectOtherStudent(@Param("studentId") Integer studentId, @Param("topicId") Integer topicId);

    @Modifying
    @Query(value = "update OPtModel set teacherOpinion = -1 where topicId <>:topicId and studentId =:studentId")
    int removeStudentForTopics(@Param("studentId") Integer studentId, @Param("topicId") Integer topicId);

    @Query(value = "select new com.cn.yds.bdy.dao.model.OptPo(a.topicId,a.studentId,a.teacherOpinion ) from OPtModel a " +
            "where a.status =1 and a.studentId =:studentId ")
    List<OptPo> findOptPosByTopicIds(@Param("studentId") Integer studentId);
}
